
        
package com.mall.order.service;

import com.ctg.mse.common.utils.AsyncHttpUtil;
import com.ctg.mse.common.utils.JacksonUtil;
import com.mall.order.model.DemoAppBaseInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import org.asynchttpclient.*;
import org.springframework.stereotype.Service;

import static org.asynchttpclient.Dsl.*;

/**
 * @Description: 记录一些常用的通用操作
 * @Author: zhouzhao
 * @CreateTime: 2022/10/19 10:38:29
 */
@Service
public class BasicDemoService implements ApplicationListener<ApplicationEvent> {

    private static final Logger LOGGER = LoggerFactory.getLogger(BasicDemoService.class);

    @Autowired
    private ThreadPoolTaskScheduler taskScheduler;

    /**
     * 服务初始化后调用一次的方法
     *//*
    @PostConstruct
    public void initService() {

        LOGGER.info("initService hello");
        //每100秒触发一次
        taskScheduler.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                LOGGER.info("scheduleAtFixedRate hello");
            }
        }, Duration.ofSeconds(10));
    }*/

    /**
     * 服务完全初始化完后调用的方法
     * 这时候MySQL，redis 等都可以用使用
     *
     * @param event
     */
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            LOGGER.info("onApplicationEvent ReadyEvent");
        }
    }

    /**
     * 定时任务
     * cron表达式
     * <p>
     * http 异步执行demo
     */
    public void httpDemo() {
        int size = 200;
        List<RequestBuilder> requests = new ArrayList<>();
        //同时请求快速http 请求
        DemoAppBaseInfoVo input = new DemoAppBaseInfoVo();
        input.setName("hello");
        String inputStr = JacksonUtil.encode(input);
        for (int i = 0; i < size; i++) {
            requests.add(post("http://10.215.40.137:7071/app/fastPost").setBody(inputStr));
        }
        long start = System.currentTimeMillis();
        //批量异步请求 同步获取String结果
        List<String> resStrlist = AsyncHttpUtil.SYNCBatch(requests);
        LOGGER.info("SYNCBatch " + requests.size() + "all done use:" + (System.currentTimeMillis() - start));
        size = 100;
        requests = new ArrayList<>();
        //同时请求快速http 请求
        for (int i = 0; i < size; i++) {
            requests.add(get("http://10.215.40.137:7071/app/fastGet").addQueryParam("say", "hello"));
            requests.add(get("http://10.215.40.137:7071/app/fastGet?say=hi"));
        }
        start = System.currentTimeMillis();
        //批量异步请求 同步获取Responce结果
        List<Response> reslist = AsyncHttpUtil.syncBatch(requests);

        LOGGER.info("syncBatch " + requests.size() + "all done use:" + (System.currentTimeMillis() - start));

        //批量异步请求，异步获取Respon结果
        LOGGER.info("asyncBatch:" + requests.size());
        start = System.currentTimeMillis();
        List<ListenableFuture<Response>> futures = AsyncHttpUtil.asyncBatch(requests);
        LOGGER.info("asyncBatch sended use:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        reslist = AsyncHttpUtil.waitAllDone(futures);
        LOGGER.info("asyncBatch waitAllDone use:" + (System.currentTimeMillis() - start));
        //单个请求同步
        start = System.currentTimeMillis();
        AsyncHttpUtil.sync(get("http://10.215.40.137:7071/app/fastGet"));
        LOGGER.info("sync use:" + (System.currentTimeMillis() - start));
        //单个请求异步
        ListenableFuture<Response> future = AsyncHttpUtil.async(get("http://10.215.40.137:7071/app/fastGet"));
        start = System.currentTimeMillis();
        //waitDone 是返回 Response
        String res = AsyncHttpUtil.WAITDone(future);
        LOGGER.info("async WAITDone use:" + (System.currentTimeMillis() - start));
    }

    /**
     * 定时任务
     * 服务启动后延迟 1s 启动
     * 之后10s 触发一次
     */
    /*@Scheduled(initialDelay = 1000, fixedRate = 6000)  //每隔5秒执行一次定时任务
    public void fixedRate() {

        LOGGER.info("fixedRate hello");
    }*/

    /**
     * 基本测试
     *
     * @param echo
     * @return
     */
    public String testEcho(String echo) {
        LOGGER.debug("%s testEcho test: %d ", "debug", 5);
        return echo;
    }


}
